package azalea.mqtt.message.builder;

import java.io.UnsupportedEncodingException;

import azalea.mqtt.message.builder.impl.SubscribeDataImpl;
import azalea.mqtt.message.utils.ByteUtils;
import azalea.mqtt.message.utils.MqttStrUtils;

public class SubscribeMsgBuilder {
	
	/**
	 * 82 头 |0F 9+6=F 长度 |74 A2  包唯一标识 | 00 v5版本的 |00 09 长度| 6D 79 74 6F 70 69 63 2F 23  mytopic/# | 00  qos
	 * 82 0F 74 A2 00 00 09 6D 79 74 6F 70 69 63 2F 23 00  
	 * mytopic/# 
     * 90 04 74 A2 00 00 
     * 
     * 82 0F 74 A4 00 00 09 6D 79 74 6F 70 69 63 2F 23 01
     * 90 04 74 A4 00 01 
     * 
     * 90 03 00 17 00 
     * 
     * 取消订阅
     * A2 0E 74 A5 00 00 09 6D 79 74 6F 70 69 63 2F 23
     * B0 03 74 A5 00 
     * qos2 订阅
     * 82 0F 74 A6 00 00 09 6D 79 74 6F 70 69 63 2F 23 02
     * 90 04 74 A6 00 02 
     * 82 0D 70 C2 00 00 07 78 78 78 2F 64 64 64  02
     * 
     * 82 0C 47 E7 00 07 78 78 78 2F 64 64 64 02
     * 
     * 82 10 00 17 00 0B 61 2F 62 3B 63 2F 64 3B 65 2F 66 00 
     * 
     * 82 10 47 E9 00 0B 61 2F 62 3B 63 2F 64 3B 65 2F 66 00   a/b;c/d;e/f
     * 82 10 47 EB 00 0B 61 2F 62 7C 63 2F 64 7C 65 2F 66 00   a/b|c/d|e/f
     * 82 14 47 ED 00 03 61 2F 62 00 00 03 63 2F 64 00 00 03 65 2F 66 00 a/b,c/d,e/f
     * 
     * 82 头| 14 长度| 47 ED 唯一标识 |00 03 长度| 61 2F 62 a/b| 00 qos| 00 03 长度 63 2F 64 c/d| 00  qos| 00 03  长度| 65 2F 66 e/f |00 qos
	 * 
	 * 
	 */
	
	public static byte[] subscribe(int identifier,Topic ...topics) {
		int len=2;
		for(Topic t:topics) {
			byte[] bt=MqttStrUtils.getUTF8Bytes(t.getTopic());
			len+=2;//
			len+=bt.length;
			len+=1;//qos
		}
		byte[] payload=new byte[len+10];
		int index=0;
		int count=0;
		for(Topic t:topics) {
			byte[] bt=MqttStrUtils.getUTF8Bytes(t.getTopic());
			int topiclen=bt.length;
			payload[index]=(byte)(topiclen/256);
			payload[index+1]=(byte)(topiclen%256);
			index+=2;
			System.arraycopy(bt, 0, payload, index, topiclen);
			index+=topiclen;
			payload[index]=(byte)t.getQos();
			index+=1;
			count=count+2+1+topiclen;
		}
		return SubscribeDataImpl.subscribe((byte)((identifier>>8)&0xff), (byte)(identifier&0xff),payload, count);
	}
	public static byte[] suback(int identifier,SubscribeCode ...codes) {
		//0x00 - 成功 - 最大QoS为0 0x01 - 成功 - 最大QoS为1 0x02 - 成功 - 最大QoS为2 0x80 - 失败
		byte[] payload=new byte[codes.length];
		for(int i=0;i<codes.length;++i) {
			SubscribeCode c=codes[i];
			switch(c) {
			case successQ0:
				payload[i]=0x00;
				break;
			case successQ1:
				payload[i]=0x01;
				break;
			case successQ2:
				payload[i]=0x02;
				break;
			default:
				payload[i]=(byte)0x80;
				break;
			}
			
		}
		return SubscribeDataImpl.suback((byte)((identifier>>8)&0xff), (byte)(identifier&0xff),payload, payload.length);
	}
	public static byte[] unsubscribe (int identifier,String... topics) {
		int totalLen=0;
		for(String s:topics) {
			totalLen+=MqttStrUtils.getUTF8Bytes(s).length;
			totalLen+=2;
		}
		byte[] payload=new byte[totalLen+1];
		int startIndex=0;
		for(String s:topics) {
			byte[] strbytes=MqttStrUtils.getUTF8Bytes(s);
			int len=strbytes.length;
			byte[] lenbyte= {(byte)(len>>8&0xff),(byte)(len&0xff)};
			ByteUtils.copy(lenbyte, payload, 0, startIndex, 2);
			startIndex+=2;
			ByteUtils.copy(strbytes, payload, 0, startIndex, len);
			startIndex+=strbytes.length;
		}
		return SubscribeDataImpl.unsubscribe((byte)((identifier>>8)&0xff), (byte)(identifier&0xff), payload, totalLen);
	}
	public static byte[] unsuback (int identifier) {
		return SubscribeDataImpl.unsuback((byte)((identifier>>8)&0xff), (byte)(identifier&0xff));
	}
	
	/*
	 * public static void main(String[] args) throws UnsupportedEncodingException {
	 * //byte[] bs=SubscribeData.suback((byte)((23>>8)&0xff),
	 * (byte)(23&0xff),"xxx/ddd".getBytes("utf-8"),
	 * "xxx/ddd".getBytes("utf-8").length); //byte[]
	 * bs=suback(23,SubscribeCode.successQ0); byte[] bs=subscribe(23,new
	 * Topic("a/b;c/d;e/f",0)); char[] cs="0123456789ABCDEF".toCharArray(); for(byte
	 * b:bs) { int cccH=(b& 0xFF)>>4; System.out.print(cs[cccH]); int cccL=b&0x0f;
	 * System.out.print(cs[cccL]); System.out.print(" "); }
	 * 
	 * }
	 */
}
